<template>
  <div>
    <el-table v-loading="loading" :data="dataSource" @row-click="handleRowClick" :height="autoHeight">
      <el-table-column prop="requestName" label="请求名称" align="center">
        <template slot="header" slot-scope="scope">
          <div>{{ scope.column.label }}</div>
          <el-input v-model="filterModel[scope.column.property]" :placeholder="scope.column.label + '过滤'" clearable @change="filterChange(scope.column.property)"></el-input>
        </template>
      </el-table-column>
      <el-table-column prop="callCmd" label="请求指令" align="center">
        <template slot="header" slot-scope="scope">
          <div>{{ scope.column.label }}</div>
          <el-input v-model="filterModel[scope.column.property]" :placeholder="scope.column.label + '过滤'" clearable @change="filterChange(scope.column.property)"></el-input>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="80px">
        <template slot="header">
          <el-button type="primary" @click="visible = true" icon="el-icon-circle-plus-outline"></el-button>
        </template>
        <div slot-scope="scope">
          <el-button type="danger" icon="el-icon-delete" circle @click="removeData(scope.row, scope.$index)" title="删除"></el-button>
        </div>
      </el-table-column>
    </el-table>
    <div class="lab-table-page">
      <div style="display: inline-flex">
        <el-pagination
            :disabled="loading"
            :current-page="pageInfo.page"
            :page-sizes="[5, 10, 15, 20, 50, 100]"
            :page-size="pageInfo.pageSize"
            layout="total, sizes, jumper"
            :total="pageInfo.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
        />
        <el-button-group style="padding: 2px 5px">
          <el-button icon="el-icon-refresh" title="刷新" :disabled="loading" @click="loadSource([])" />
        </el-button-group>
      </div>
    </div>
    <el-dialog :visible.sync="visible" title="请求信息" width="600px">
      <el-form label-width="80px" label-position="right">
        <el-form-item label="请求名称">
          <el-input v-model="entityInfo.requestName"></el-input>
        </el-form-item>
        <el-form-item label="调用指令">
          <el-input v-model="entityInfo.callCmd"></el-input>
        </el-form-item>
        <el-form-item label="请求类型">
          <el-select v-model="entityInfo.requestType" placeholder="请求类型" style="width: 100%" clearable>
            <el-option v-for="item in requestTypes" :key="item.value" :value="item.value" :label="item.label" />
          </el-select>
        </el-form-item>
        <el-form-item label="返回类型">
          <el-select v-model="entityInfo.resultType" placeholder="返回类型" style="width: 100%" clearable>
            <el-option v-for="item in resultTypes" :key="item.value" :value="item.value" :label="item.label" />
          </el-select>
        </el-form-item>
        <el-form-item label="请求地址">
          <el-input v-model="entityInfo.requestUrl"></el-input>
        </el-form-item>
        <el-form-item label="内容类型">
          <el-select v-model="entityInfo.contentType" placeholder="请求体类型" style="width: 100%" clearable>
            <el-option v-for="item in contentTypes" :key="item.value" :value="item.value" :label="item.label" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-circle-check" type="success" @click="saveData(entityInfo)">{{ entityInfo.requestId ? '更新' : '保存' }}</el-button>
          <el-button icon="el-icon-circle-close" type="info" @click="visible = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import IdeaTable from "@/mixins/IdeaTable";

export default {
  name: "RequestTable",
  mixins: [IdeaTable],
  created() {
    this.queryUrl = '/request/queryPage'
    this.saveUrl = '/request/save'
    this.removeUrl = '/request/delete/'
    this.rowKey = 'requestId'
    this.loadSource()
  },
  data() {
    return {
      requestTypes: [
        { value: 'get', label: 'GET' },
        { value: 'post', label: 'POST' },
        { value: 'put', label: 'PUT' },
        { value: 'delete', label: 'DELETE' },
      ],
      resultTypes: [
        { value: 'String', label: '字符串' },
        { value: 'json', label: 'JSON' },
        { value: 'byte', label: '字节数组' },
      ],
      contentTypes: [
        { value: 'application/json; charset=utf-8', label: '对象' },
        { value: 'application/x-www-form-urlencoded', label: '表单' },
        { value: 'multipart/form-data', label: '文件' },
      ]
    }
  },
  computed: {
    autoHeight() {
      return `calc(100vh - 130px)`
    }
  },
  methods: {
    initEntity(){
      this.entityInfo = { dataState: 0 }
    }
  }
}
</script>

<style scoped>

</style>
